package com.lepu.app.fun.chat.util;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.CallBack;
import com.lepu.app.fun.chat.bean.BeanLectureChatUser;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wxd on 2016/7/11.
 */
public class LectureUtil implements AsyncRequest {

    //这个类处理回调
    public static final String REQUEST_ALL_GROUP_INFO = "request_all_group_info";
    private static final String REQUEST_SIMPLE_GROUP_INFO = "request_simple_group_info";

    private static LectureUtil mInstance = null;

    private BaseFragmentActivity mBaseFragmentActivity = null;
    private CallBack mCallBack = null;

    private HashMap<String,BeanLectureChatUser> mGroupChatUserIDMap = new HashMap<String,BeanLectureChatUser>();
    private HashMap<String,String> mGroupChatUserNameMap = new HashMap<String,String>();

    private LectureUtil() {

    }

    public static LectureUtil getInstance() {
        if (mInstance == null) {
            mInstance = new LectureUtil();
        }
        return mInstance;
    }

    // ================================================================================
    public BeanLectureChatUser getGroupChatUser(String UserID){
        BeanLectureChatUser item = mGroupChatUserIDMap.get(UserID);
        if(item != null)
        {
            return item;
        }else{
            return null;
        }
    }

    public String getUserIDsByContent(String sendContent)
    {
        StringBuilder sb = new StringBuilder();
        if(!TextUtils.isEmpty(sendContent)){
            String[] arrayContent = sendContent.split("\u200B");
            if(arrayContent != null && arrayContent.length > 1){
                for(int i=0;i<arrayContent.length;i++){
                    String content = arrayContent[i];
                    String[] arrayName = content.split("@");

                    if(arrayName != null && arrayName.length > 1){
                        String name = arrayName[arrayName.length-1];
                        if(!TextUtils.isEmpty(name)){
                            sb.append(mGroupChatUserNameMap.get(name));
                            sb.append(",");
                        }
                    }
                }
                sb.deleteCharAt(sb.length()-1);
            }
        }
        return sb.toString();
    }

    public void clear()
    {
        mGroupChatUserIDMap.clear();
        mGroupChatUserNameMap.clear();
    }

    /***
     *
     * @param baseFragmentActivity
     * @param GroupID
     */
    public void requestAllGroupInfo(BaseFragmentActivity baseFragmentActivity,
                                    String GroupID,CallBack callBack) {

        mBaseFragmentActivity = baseFragmentActivity;
        mCallBack = callBack;

        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject params = new JSONObject();

        try {
            params.put("DeviceID", Setting.DEVICEID);
            params.put("ApplicationID", Setting.APPLICATIONID);

            params.put("UserID", LoginUtil.getUserId());
            params.put("LoginToken", LoginUtil.getLoginToken());

            params.put("GroupID", GroupID);
        } catch (Exception e) {
        }
        map.put("ht", params);

        ApiClient.http_post(Setting.getGroupMemberListUrl(), map, null, this,
                REQUEST_ALL_GROUP_INFO, true, false, true);
    }

    public void requestAllGroupInfo(BaseFragmentActivity baseFragmentActivity,
                                    String GroupID,AsyncRequest asyncRequest) {

        mBaseFragmentActivity = baseFragmentActivity;

        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject params = new JSONObject();

        try {
            params.put("DeviceID", Setting.DEVICEID);
            params.put("ApplicationID", Setting.APPLICATIONID);

            params.put("UserID", LoginUtil.getUserId());
            params.put("LoginToken", LoginUtil.getLoginToken());

            params.put("GroupID", GroupID);
        } catch (Exception e) {
        }
        map.put("ht", params);

        ApiClient.http_post(Setting.getGroupMemberListUrl(), map, null, asyncRequest,
                REQUEST_ALL_GROUP_INFO, true,false,true);
    }

    /***
     *
     * @param baseFragmentActivity
     * @param TargetUserID
     */
    public void requestSimpleGroupInfo(BaseFragmentActivity baseFragmentActivity,
                                       String TargetUserID,String GroupID,CallBack callBack) {

        mBaseFragmentActivity = baseFragmentActivity;
        mCallBack = callBack;

        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject params = new JSONObject();

        try {
            params.put("DeviceID", Setting.DEVICEID);
            params.put("ApplicationID", Setting.APPLICATIONID);

            params.put("UserID", LoginUtil.getUserId());
            params.put("LoginToken", LoginUtil.getLoginToken());

            params.put("TargetUserID", TargetUserID);
            params.put("GroupID", GroupID);
        } catch (Exception e) {
        }
        map.put("ht", params);

        ApiClient.http_post(Setting.getGroupMemberDetailUrl(), map, null, this,
                REQUEST_SIMPLE_GROUP_INFO, true,false,true);
    }

    // ================================================================================
    @Override
    public void RequestComplete(Object object, Object data) {
        if (object.equals(REQUEST_ALL_GROUP_INFO)) {

            ArrayList<BeanLectureChatUser> dataArraylist = BeanLectureChatUser
                    .parseListInfo((String) data);

            if(dataArraylist != null && dataArraylist.size() > 0){
                for(BeanLectureChatUser item : dataArraylist){
                    mGroupChatUserIDMap.put(item.UserID,item);
                    mGroupChatUserNameMap.put(item.NickName,item.UserID);
                }
            }

            if(mCallBack != null)
            {
                mBaseFragmentActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mCallBack.callBackSuccess(null);
                    }
                });
            }

        } else if (object.equals(REQUEST_SIMPLE_GROUP_INFO)) {

            final BeanLectureChatUser item = BeanLectureChatUser.parseDetailInfo((String)data);

            if(item != null)
            {
                if(!mGroupChatUserIDMap.containsKey(item.UserID))
                {
                    mGroupChatUserIDMap.put(item.UserID,item);
                    mGroupChatUserNameMap.put(item.NickName,item.UserID);

                    if(mCallBack != null)
                    {
                        mBaseFragmentActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mCallBack.callBackSuccess(item);
                            }
                        });
                    }
                }
            }
        }
    }

    @Override
    public void RequestError(Object object, int errorId, String data) {
        if (object.equals(REQUEST_ALL_GROUP_INFO)) {
            if(mCallBack != null)
            {
                mBaseFragmentActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mCallBack.callBackFail(null);
                    }
                });
            }
        } else if (object.equals(REQUEST_SIMPLE_GROUP_INFO)) {
            if(mCallBack != null)
            {
                mBaseFragmentActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mCallBack.callBackFail(null);
                    }
                });
            }
        }
    }
}
